{% extends 'course-video.html' %}

{% load staticfiles %}
{% block title %}课程评论{% endblock %}
{% block custom_bread %}
    <section>
        <div class="wp">
            <ul class="crumbs">
                <li><a href="/">首页</a>></li>
                <li><a href="{% url 'course:course_list' %}">公开课程</a>></li>
                <li><a href="{% url 'course:course_detail' course.id %}">课程详情</a>></li>
                <li>课程评论</li>
            </ul>
        </div>
    </section>
{% endblock %}

{% block left_content %}
    <!--展示页切换-->
    <div class="mod-tab-menu">
        <ul class="course-menu clearfix">
            <li><a class="" id="learnOn"  href="{% url 'course:course_info' course.id %}"><span>章节</span></a></li>
            <li><a class="ui-tabs-active active" id="commentOn" href="{% url 'course:course_comments' course.id %}"><span>评论</span></a></li>
        </ul>
    </div>
    <!--发布评论-->
    <div id="js-pub-container" class="issques clearfix js-form">
        <div class="wgt-ipt-wrap pub-editor-wrap " id="js-pl-input-fake">
            <textarea id="js-pl-textarea" class="" placeholder="扯淡、吐槽、表扬、鼓励……想说啥就说啥！" ></textarea>
        </div>
        <input type="button" id="js-pl-submit" class="pub-btn" data-cid="452" value="发表评论">
        <p class="global-errortip js-global-error"></p>
    </div>
    <!--显示评论-->
    <div id="course_note">
        <ul class="mod-post" id="comment-list">
            {% for comment in all_comments %}
            <li class="post-row">
                <div class="media">
                    <span target="_blank"><img src='{{ MEDIA_URL }}{{ comment.user.image }}' width='40' height='40' /></span>
                </div>
                <div class="bd">
                    <div class="tit">
                        <span target="_blank">{{ comment.user.nick_name }}</span>
                    </div>
                    <p class="cnt">{{ comment.comments }}</p>
                    <div class="footer clearfix">
                        <span title="创建时间" class="l timeago">时间：{{ comment.add_time }}</span>
                    </div>
                </div>
            </li>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

{% block custom_js %}
    <!--添加评论-->
    <script type="text/javascript">
     $("#js-pl-submit").keydown(function(event){
        if(event.keyCode == 13){
             $('#js-pl-submit').trigger('click');
        }
    });

    $('#js-pl-submit').on('click', function(){
        var comments = $("#js-pl-textarea").val();
        if(comments == ""){
            alert("评论不能为空");
            return
        }
        $.ajax({
            cache: false,
            type: "POST",
            url:"{% url 'course:add_comment' %}",
            data:{'course_id':{{ course.id }}, 'comments':comments},
            async: true,
            beforeSend:function(xhr, settings){
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
            },
            success: function(data) {
                if(data.status == 'fail'){
                    if(data.msg == '用户未登录'){
                        window.location.href="login.html";
                    }else{
                        alert(data.msg)
                    }

                }else if(data.status == 'success'){
                    window.location.reload();//刷新当前页面.
                }
            },
        });
    });
</script>
{% endblock %}